<?php
namespace app\common\controller;

use app\common\model\Gameulog;
use app\common\model\Player;
use think\Controller;
use think\Db;
use think\cache\driver\Redis;
use think\facade\Log;
use think\facade\Session;
use app\common\model\Gameagent;

/**
 * 后台公用基础控制器
 * Class AdminBase
 * @package app\common\controller
 */
class FaXingBase extends Controller
{
    protected $game;
    protected $cid = 0;
    protected $gid = 0;
    protected $token= 0;
    protected $channel;
    protected $extdata = [];
    protected $account;
    protected function initialize()
    {
        $data = $this->request->request();
        $route = $this->request->route();
        $data = array_merge($data, $route);

        if(empty($data['gid'])){
            echo "<script>alert('gid参数错误！')</script>";die();
        }
        $game = DB::connect('db_config_aiqi')->name('gamelist')
            ->whereIn('gstatus',[1,2])
            ->where(['id'=>$data['gid']])
            ->find();

        if(empty($game)){
            echo "<script>alert('游戏不存在！')</script>";die();
        }
        $channel = DB::connect('db_config_aiqi')->name('channel')
            ->field('id,username,platform')
            ->where(array('id'=>$data['cid'],'status'=>1))
            ->find();

        if(empty($channel)){
            echo "<script>alert('渠道不存在！')</script>";die();
        }

        $gameArgnet = DB::connect('db_config_aiqi')->name('gameagent')
            ->where(['cid'=>$data['cid'],'gid'=>$data['gid'],'status'=>1])
            ->find();
        if(empty($gameArgnet)){
            echo "<script>alert('该渠道未代理此游戏！')</script>";die();
        }
        if(empty($gameArgnet['extdata'])){
            echo "<script>alert('对接参数未设置！')</script>";die();
        }

        $this->assign('extdata',$gameArgnet['extdata']);


        $this->extdata = json_decode($gameArgnet['extdata'],true);
        $this->cid = $channel['id'];
        $this->gid = $game['id'];
        $this->game = $game;
        $this->channel = $channel;
        $this->assign('cid',$this->cid);
        $this->assign('gid',$this->gid);
        $this->assign('token',$this->token);
        parent::initialize();

    }

    protected function checkLogin(){
        $token = $this->request->param('token') ?? $this->token;

        $redis = new Redis();
        if($token){
            $player = $redis->get($token);
            if(!empty($player['account'])){
                $this->player = $player;
                $this->account = $player['account'];
                $this->uid = $player['id'];
                $this->token = $player['token'];
                return $this->uid;
            }
        }
    }

    /**
     * @param $sign 签名
     * @param $type 类型 默认1登录 2 支付
     * @return bool
     */
    protected function checkSign($data,$type = 1){
        if($type==1){
            $key = $this->game['login_key'];
        }else{
            $key = $this->game['pay_key'];
        }
        $sign = $data['sign'];
        unset($data['sign']);
        $check_sign = get_sign($data,$key);
        if($check_sign==$sign){
            return true;
        }
        return false;
    }






    /**
     * 防沉迷验证
     * @return bool
     */
    protected function check_player_play_time()
    {
        $palyer = $this->player;

        if(empty($palyer)){
            return true;
        }
        if($palyer['is_age'] == 0){
            return true;
        }
        if ($palyer['is_age'] == 2) { // 已成年
            return true;
        }
        // 未成年玩家只能在周五、周六、周日和法定节假日的20点到21点开始游戏
        $week = date('N', time());
        $hour = date('H', time());
        if(in_array($week,[5,6,7]) && $hour>=20 && $hour<=21 && is_hoilday()){
            return true;
        }
        return false;
    }



}